package cn.myweixin.util;




import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class Office2PDFUtil {
	
	public static final String[] OFFICE_POSTFIXS = {"doc", "docx", "xls","xlsx", "ppt", "pptx"};
	
	
	private static void converterFile(String sourceFile, String destFile) {    
        Process pro = null;    
        OpenOfficeConnection connection = null;    
        try {    
        	File inputFile = new File(sourceFile);    
        	   //文件夹不存在创建目录    
            File outputFile = new File(destFile);    
            if (!outputFile.getParentFile().exists()) {    
                outputFile.getParentFile().mkdirs();    
            }    
    		
            String OpenOffice_HOME = "G:\\Environment\\OpenOffice 4";    
            if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {    
                OpenOffice_HOME += "\\";    
            }    
            // 启动OpenOffice的服务      
            String  command = OpenOffice_HOME+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";   
            pro = Runtime.getRuntime().exec(command);    
            // connect to an OpenOffice.org instance running on port 8100    
            connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);    
            connection.connect();    
            // convert    
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);    
            converter.convert(inputFile, outputFile);    
            
        } catch (Exception e) {    
            e.printStackTrace();    
              
        }finally{    
            if(connection!=null){    
                // close the connection    
                connection.disconnect();    
            }    
             // 关闭OpenOffice服务的进程      
            if(pro!=null){    
                pro.destroy();    
            }    
        }    
          
    }     
	
	
	public static boolean office2PDF(String inputFilePath, String outputFilePath){
		  
		   boolean flag = false;
	       File inputFile = new File(inputFilePath);
	       ArrayList<String> office_Formats = new ArrayList<String>();
	       Collections.addAll(office_Formats, OFFICE_POSTFIXS);
	       //获取文件名
	       String realName = inputFile.getName().substring(inputFile.getName().lastIndexOf(File.separator)+1);
	       
	    
		
	       if ((null != inputFilePath) && (inputFile.exists()))
	       {
	           // 判断目标文件路径是否为空
	           if (office_Formats.contains(getPostfix(inputFilePath)))
	           {
	                  // 转换后的文件路径
	            	 String outputFilePath_new=outputFilePath+File.separator+realName.toLowerCase().replaceAll("."
	                         + getPostfix(inputFilePath), ".pdf");
	                  /*String outputFilePath_new = inputFilePath.toLowerCase().replaceAll("."
	                         + getPostfix(inputFilePath), ".pdf");*/
	                  converterFile(inputFilePath, outputFilePath_new);
	                  flag = true;
	           }
	       }
	       
	       return flag;
	
	}
	
	

	
	/**

     * 获取文件的后缀名

     */
    private static String getPostfix(String inputFilePath)
    {
       String[] p = inputFilePath.split("\\.");
       if (p.length > 0)
           return p[p.length - 1];
        else
           return null;
    }
    
    
    
}
